package com.ranyk.www.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * CLASS_NAME: EhcacheConfig.java <br/>
 *
 * @author ranyk           <br/>
 * @version V1.0           <br/>
 * @decription: 缓存配置  <br/>
 * @date: 2022-04-14 <br/>
 */
@Configuration
public class EhcacheConfig {

    /**
     * 配置 {@link EhCacheManagerFactoryBean} bean 对象
     *
     * @return 返回配置好的  {@link EhCacheManagerFactoryBean} bean 对象
     */
    @Bean
    public EhCacheManagerFactoryBean ehcache() {
        EhCacheManagerFactoryBean ehCache = new EhCacheManagerFactoryBean();
        Resource resource = new ClassPathResource("config/ehcache.xml");
        ehCache.setConfigLocation(resource);
        ehCache.setShared(true);
        return ehCache;
    }

    /**
     * 配置 EhCacheCacheManager 对象
     *
     * @param ehcache 传入 {@link EhCacheManagerFactoryBean} bean 对象
     * @return 返回配置好的 {@link EhCacheCacheManager} bean 对象
     */
    @Bean
    public EhCacheCacheManager cacheManager(@Qualifier("ehcache") EhCacheManagerFactoryBean ehcache) {
        EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
        ehCacheCacheManager.setCacheManager(ehcache.getObject());
        return ehCacheCacheManager;
    }

}
